﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using System.Runtime.Serialization;

namespace EzDesk.Apps.Model.Message
{
    [DataContract]
    public class Topic:MessageContent
    {
        /// <summary>
        /// 消息发送者
        /// </summary>
        [DataMember]
        public SimpleUser Sender { get; set; }

        /// <summary>
        /// 当前接受消息的用户
        /// </summary>
        [DataMember]
        public SimpleUser Recipient { get; set; }

        /// <summary>
        /// 需要确认
        /// </summary>
        [DataMember]
        public bool NeedConfirm { get; set; }

        /// <summary>
        /// 获取消息的人是否已经确认过了
        /// </summary>
        public bool? HasConfirmed
        {
            get
            {
                bool? result = null;
                // 需要确认 并且接受的不是自己的消息，则判断是否确认过
                if (NeedConfirm && Recipient.Id != Sender.Id)
                {
                    result = false;
                    foreach (MessageConfirm confirm in ConfirmedInfo)
                    {
                        if (confirm.UserId == Recipient.Id)
                        {
                            result = true;
                        }
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 回复列表
        /// </summary>
        [DataMember]
        public List<SingleMessage> ReplyList { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [DataMember]
        public DateTime CreateOn { get; set; }

        /// <summary>
        /// 参与者信息
        /// </summary>
        [DataMember]
        public ParticipatorsInfo Participator { get; set; }

        IList<SimpleUser> _allConfirmUsers;

        public IList<SimpleUser> AllConfirmUsers
        {
            get
            {
                if (!NeedConfirm) return null;

                if (_allConfirmUsers == null)
                {
                    _allConfirmUsers = getAllConfirmUsers();
                }
                return _allConfirmUsers;
            }
        }

        private IList<SimpleUser> getAllConfirmUsers()
        {
            // 除了自己以外的所有用户 都必须确认

            var userList = new List<SimpleUser>();

            foreach (var user in Participator.AllUserMemebers)
            {
                if (user.Id != Sender.Id)
                {
                    userList.Add(user);
                }
            }
            return userList;
        }



        /// <summary>
        /// 已经确认的用户列表
        /// </summary>
        [DataMember]
        public List<MessageConfirm> ConfirmedInfo { get; set; }

        private IList<SimpleUser> _noConfirmUsers;
        private IList<SimpleUser> getNoConfirmUsers()
        {
            var userList = new List<SimpleUser>();
            foreach (var user in AllConfirmUsers)
            {
                bool flag = false;
                foreach (MessageConfirm confirm in ConfirmedInfo)
                {
                    if (confirm.UserId == user.Id)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag) // 没有包含在确认列表中
                {
                    userList.Add(user);
                }
            }
            return userList;
        }
        /// <summary>
        /// 没有确认的用户列表
        /// </summary>
        public IList<SimpleUser> NoConfirmUsers
        {
            get
            {
                if (!NeedConfirm) return null;

                if (_noConfirmUsers == null)
                {
                    _noConfirmUsers = getNoConfirmUsers();
                }
                return _noConfirmUsers;
            }
            set
            {
                throw new Exception("NoConfirmUsers 不能set");
            }
        }

        /// <summary>
        /// 没有确认的人数
        /// </summary>
        public int NoConfirmCount
        {
            get
            {
                if (!NeedConfirm || NoConfirmUsers == null) return 0;
                return NoConfirmUsers.Count;
            }
        }




        private IList<SimpleUser> _confirmdUsers;

        private IList<SimpleUser> getConfirmedUsers()
        {
            var userList = new List<SimpleUser>();
            foreach (var user in AllConfirmUsers)
            {
                foreach (MessageConfirm confirm in ConfirmedInfo)
                {
                    if (confirm.UserId == user.Id)
                    {
                        userList.Add(user);
                    }
                }
            }
            return userList;
        }

        /// <summary>
        /// 已经确认的用户列表
        /// </summary>
        public IList<SimpleUser> ConfirmUsers
        {
            get
            {
                if (!NeedConfirm) return null;

                if (_confirmdUsers == null)
                {
                    _confirmdUsers = getConfirmedUsers();
                }
                return _confirmdUsers;
            }
            set
            {
                throw new Exception("ConfirmUsers 不能set");
            }
        }


        /// <summary>
        /// 已确认人数
        /// </summary>
        public int ConfirmedCount
        {
            get
            {
                if (!NeedConfirm || ConfirmedInfo == null) return 0;

                return ConfirmedInfo.Count;
            }
        }






    }
}
